package com.ruoyi.dataserver.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import com.ruoyi.dataserver.model.line.LINE;
import com.ruoyi.dataserver.service.ILxAnalyzeAnalyzeService;
import com.ruoyi.dataserver.service.ISearchTableService;
import com.ruoyi.dataserver.vo.PgLineEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


/**
 * @ProjectName
 * @Package com.lab.dataserver.service.impl
 * @ClassName TopologyAnalyzeServceImpl
 * @Description 拓扑分析逻辑层
 * @Company lab
 * @Author wangzhx
 * @Date 2022/2/18 0009 11:55
 * @UpdateUser
 * @UpdateDate
 * @UpdateRemark
 * @Version 1.0.0
 */
@Slf4j
@Component
public class LxAnalyzeServceImpl implements ILxAnalyzeAnalyzeService {

    @Autowired
    public JdbcTemplate jdbcTemplate;

    @Autowired
    public ISearchTableService tableService;


    @Override
    public List lxAnalyze(String dm) {
        StringBuffer sb = new StringBuffer();
        String linetable = tableService.searchLineTable(dm);
        String sql = sb.append("select * from \"").append(linetable).append("\" where dm='").append(dm).append("'").toString();
        RowMapper<LINE> rowMapper1  = new BeanPropertyRowMapper<LINE>(LINE.class);
        List<LINE> lineList = jdbcTemplate.query(sql,rowMapper1);
        /*lineList.stream().map(wsgline -> {
            String[] SNODID =  wsgline.getSNODID().split("-");
            Double[] SNODIDDouble = new Double[SNODID.length];
            String[] ENODID = wsgline.getENODID().split("-");
            Double[] ENODIDDouble = new Double[ENODID.length];
            // 坐标类型转换
            for (int i = 0; i < SNODID.length; i++) {
                SNODIDDouble[i] = Double.valueOf(SNODID[i]);
            }
            for (int i = 0; i < ENODID.length; i++) {
                ENODIDDouble[i] = Double.valueOf(ENODID[i]);
            }
            wsgline.setENODIDDouble(ENODIDDouble);
            wsgline.setSNODIDDouble(SNODIDDouble);
            return wsgline;
        }).collect(Collectors.toList());*/
        return lineList;
    }

    @Override
    public List<PgLineEntity> zdmAnalyse(PgLineEntity pgLineEntity) {
        Assert.notNull(pgLineEntity, "管线不允许为空");
        Assert.notNull(pgLineEntity.getDm(), "当前管线不包含代码字段");
        Assert.notNull(pgLineEntity.getQddh(), "当前管线不包含起点点号");
        Assert.notNull(pgLineEntity.getZddh(), "当前管线不包含终点点号");


        List<PgLineEntity> beforeLine = new ArrayList<>();
        List<PgLineEntity> afterLine = new ArrayList<>();
        PgLineEntity beforeCurrentLine = pgLineEntity;
        PgLineEntity afterCurrentLine = pgLineEntity;

        for (int i = 0; i < 2; i++) {
            if (beforeCurrentLine != null) {
                beforeCurrentLine = getOneUpstreamLine(beforeCurrentLine);
                if (beforeCurrentLine != null) {
                    beforeLine.add(beforeCurrentLine);
                }
            }
            if (afterCurrentLine != null) {
                afterCurrentLine = getOneDownstreamLine(afterCurrentLine);
                if (afterCurrentLine != null) {
                    afterLine.add(afterCurrentLine);
                }
            }
        }
        List<PgLineEntity> result = CollectionUtil.reverseNew(beforeLine);
        result.add(pgLineEntity);
        result.addAll(afterLine);
        return result;
    }

    public PgLineEntity getOneDownstreamLine(PgLineEntity zhswGXLine){
        if("0".equals(zhswGXLine.getLx())){
            StringBuffer sb = new StringBuffer();
            String linetable = tableService.searchLineTable(zhswGXLine.getDm());
            String sql = sb.append("select * from \"").append(linetable)
                    .append("\" where dm ='").append(zhswGXLine.getDm()).append("'")
                    .append(" and lx = '0' ")
                    .append(" and qddh ='").append(zhswGXLine.getZddh()).append("'")
                    .toString();
            RowMapper<PgLineEntity> rowMapper1  = new BeanPropertyRowMapper<PgLineEntity>(PgLineEntity.class);
            List<PgLineEntity> lineList = jdbcTemplate.query(sql,rowMapper1);
            if(lineList.size() > 0){
                return lineList.get(0);
            }

            StringBuffer sb1 = new StringBuffer();
            String sql1 = sb1.append("select * from \"").append(linetable)
                    .append("\" where dm ='").append(zhswGXLine.getDm()).append("'")
                    .append(" and lx = '1' ")
                    .append(" and zddh ='").append(zhswGXLine.getZddh()).append("'")
                    .toString();
            List<PgLineEntity> lineList1 = jdbcTemplate.query(sql1,rowMapper1);
            if(lineList1.size() > 0){
                return lineList1.get(0);
            }
        }else{
            StringBuffer sb = new StringBuffer();
            String linetable = tableService.searchLineTable(zhswGXLine.getDm());
            String sql = sb.append("select * from \"").append(linetable)
                    .append("\" where dm ='").append(zhswGXLine.getDm()).append("'")
                    .append(" and lx = '0' ")
                    .append(" and qddh ='").append(zhswGXLine.getQddh()).append("'")
                    .toString();
            RowMapper<PgLineEntity> rowMapper1  = new BeanPropertyRowMapper<PgLineEntity>(PgLineEntity.class);
            List<PgLineEntity> lineList = jdbcTemplate.query(sql,rowMapper1);
            if(lineList.size() > 0){
                return lineList.get(0);
            }

            StringBuffer sb1 = new StringBuffer();
            String sql1 = sb1.append("select * from \"").append(linetable)
                    .append("\" where dm ='").append(zhswGXLine.getDm()).append("'")
                    .append(" and lx = '1' ")
                    .append(" and zddh ='").append(zhswGXLine.getQddh()).append("'")
                    .toString();
            List<PgLineEntity> lineList1 = jdbcTemplate.query(sql1,rowMapper1);
            if(lineList1.size() > 0){
                return lineList1.get(0);
            }
        }
        return null;
    }

    public PgLineEntity getOneUpstreamLine(PgLineEntity zhswGXLine){
        if("0".equals(zhswGXLine.getLx())){
            StringBuffer sb = new StringBuffer();
            String linetable = tableService.searchLineTable(zhswGXLine.getDm());
            String sql = sb.append("select * from \"").append(linetable)
                    .append("\" where dm ='").append(zhswGXLine.getDm()).append("'")
                    .append(" and lx = '0' ")
                    .append(" and zddh ='").append(zhswGXLine.getQddh()).append("'")
                    .toString();
            RowMapper<PgLineEntity> rowMapper1  = new BeanPropertyRowMapper<PgLineEntity>(PgLineEntity.class);
            List<PgLineEntity> lineList = jdbcTemplate.query(sql,rowMapper1);
            if(lineList.size() > 0){
                return lineList.get(0);
            }

            StringBuffer sb1 = new StringBuffer();
            String sql1 = sb1.append("select * from \"").append(linetable)
                    .append("\" where dm ='").append(zhswGXLine.getDm()).append("'")
                    .append(" and lx = '1' ")
                    .append(" and qddh ='").append(zhswGXLine.getQddh()).append("'")
                    .toString();
            List<PgLineEntity> lineList1 = jdbcTemplate.query(sql1,rowMapper1);
            if(lineList1.size() > 0){
                return lineList1.get(0);
            }
        }else{
            StringBuffer sb = new StringBuffer();
            String linetable = tableService.searchLineTable(zhswGXLine.getDm());
            String sql = sb.append("select * from \"").append(linetable)
                    .append("\" where dm ='").append(zhswGXLine.getDm()).append("'")
                    .append(" and lx = '1' ")
                    .append(" and qddh ='").append(zhswGXLine.getZddh()).append("'")
                    .toString();
            RowMapper<PgLineEntity> rowMapper1  = new BeanPropertyRowMapper<PgLineEntity>(PgLineEntity.class);
            List<PgLineEntity> lineList = jdbcTemplate.query(sql,rowMapper1);
            if(lineList.size() > 0){
                return lineList.get(0);
            }

            StringBuffer sb1 = new StringBuffer();
            String sql1 = sb1.append("select * from \"").append(linetable)
                    .append("\" where dm ='").append(zhswGXLine.getDm()).append("'")
                    .append(" and lx = '0' ")
                    .append(" and zddh ='").append(zhswGXLine.getZddh()).append("'")
                    .toString();
            List<PgLineEntity> lineList1 = jdbcTemplate.query(sql1,rowMapper1);
            if(lineList1.size() > 0){
                return lineList1.get(0);
            }
        }
        return null;
    }

}
